//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#if !defined(__OBJENUM_H__)
#define __OBJENUM_H__

class ObjectEnumeratorNode : public DLinkNode
{
public:
    virtual CARAPI_(PInterface) GetObject() = 0;

public:
    Interlocked     m_cRef;
};

class CObjectEnumerator : public IObjectEnumerator
{
public: // interface methods
    CARAPI_(PInterface) Probe(
        /* [in] */ REIID riid);

    CARAPI_(UInt32) AddRef(void);

    CARAPI_(UInt32) Release(void);

    CARAPI Aggregate(
        /* [in] */ AggrType aggrType,
        /* [in] */ PInterface pObject);

    CARAPI GetDomain(
        /* [out] */ PInterface *ppObject);

    CARAPI Current(
        /* [out] */ PInterface *ppObject);

    CARAPI MoveNext(
        /* [out] */ Boolean *pSucceeded);

    CARAPI Reset();

public:
    CObjectEnumerator(
        /* [in] */ DLinkNode *pListHead,
        /* [in] */ KMutex *pListLock,
        /* [in] */ Int32 *pListState,
        /* [in] */ PInterface pOwner);
    virtual ~CObjectEnumerator();

private:
    DLinkNode       *m_pListHead;
    KMutex          *m_pListLock;
    DLinkNode       *m_pCurrentNode;

    Int32           *m_pListState;
    Int32           m_nOrgListState;

    PInterface         m_pOwner;
    Interlocked     m_cRef;
};

inline CObjectEnumerator::CObjectEnumerator(
    /* [in] */ DLinkNode *pListHead,
    /* [in] */ KMutex *pListLock,
    /* [in] */ Int32 *pListState,
    /* [in] */ PInterface pOwner)
{
    if (pOwner) pOwner->AddRef();
    m_pOwner = pOwner;

    m_pListHead = pListHead;
    m_pCurrentNode = pListHead;

    m_pListLock = pListLock;

    m_pListState = pListState;
    m_nOrgListState = *pListState;
}

inline CObjectEnumerator::~CObjectEnumerator()
{
    if (m_pOwner) m_pOwner->Release();
}

#endif //!(__OBJENUM_H__)
